package com.pinjitsu.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

//@Component
public class RestClientServiceImpl implements RestClientService {

    private final RestTemplate restTemplate = new RestTemplate();
    private String apiKey;
    private String secret;

    @Override
    public String getAccessToken(String requestURL, String code) {
        String accessTokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + apiKey + "&redirect_uri="
                + requestURL + "&client_secret=" + secret + "&code=" + code;

        return restTemplate.getForObject(accessTokenUrl, String.class);
    }

    @Override
    public String getFriends(String accessToken) {
        String friendsUrl = "https://graph.facebook.com/me/friends?access_token=" + accessToken;
        return restTemplate.getForObject(friendsUrl, String.class);
    }

    @Override
    public String getMyProfile(String accessToken) {
        String profileUrl = "https://graph.facebook.com/me?access_token=" + accessToken;
        return restTemplate.getForObject(profileUrl, String.class);
    }

    public String getApiKey() {
        return apiKey;
    }

    @Value("${facebook.apiKey}")
    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getSecret() {
        return secret;
    }

    @Value("${facebook.secret}")
    public void setSecret(String secret) {
        this.secret = secret;
    }

}
